package org.xhnews.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

import org.xhnews.log.RaspberryLogger;

public class StringUtil {
	public static String urlEncode(String url) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < url.length(); i++) {
			switch (url.charAt(i)) {
			case '=':
				buf.append("%3d");
				break;
			case ' ':
				buf.append("%20");
				break;
			case '+':
				buf.append("%2b");
				break;
			case '\'':
				buf.append("%27");
				break;
			case '/':
				buf.append("%2F");
				break;
			case '.':
				buf.append("%2E");
				break;
			case '<':
				buf.append("%3C");
				break;
			case '>':
				buf.append("%3E");
				break;
			case '#':
				buf.append("%23");
				break;
			case '%':
				buf.append("%25");
				break;
			case '&':
				buf.append("%26");
				break;
			case '{':
				buf.append("%7B");
				break;
			case '}':
				buf.append("%7D");
				break;
			case '\\':
				buf.append("%5C");
				break;
			case '^':
				buf.append("%5E");
				break;
			case '~':
				buf.append("%73");
				break;
			case '[':
				buf.append("%5B");
				break;
			case ']':
				buf.append("%5D");
				break;
			default:
				buf.append(url.charAt(i));
				break;
			}
		}
		return buf.toString();
	}

	public static String[] split(String s, int chr) {
		Vector res = new Vector();
		int curr;
		int prev = 0;
		while ((curr = s.indexOf(chr, prev)) >= 0) {
			res.addElement(s.substring(prev, curr));
			prev = curr + 1;
		}
		res.addElement(s.substring(prev));
		String[] splitted = new String[res.size()];
		res.copyInto(splitted);
		return splitted;
	}

	public static String replaceAll(String from, String to, String source) {
		if (source == null || from == null || to == null) {
			return null;
		}
		StringBuffer bf = new StringBuffer();
		int index = -1;
		while ((index = source.indexOf(from)) != -1) {
			bf.append(source.substring(0, index) + to);
			source = source.substring(index + from.length());
			index = -1;
		}
		bf.append(source);
		return bf.toString();
	}

	public static String stringFilter(String str) {
		String now = str;
		int start = 0;
		int end = 0;
		for (int i = 0; i < now.length(); i++) {
			// '\u003C','\u003E' = <>
			if (now.charAt(i) == '\u003C') {
				start = i;
			}
			if (now.charAt(i) == '\u003E') {
				end = i;
			}
			if (start != -1 && end != -1) {
				String sub = now.substring(start, end + 1);
				str = StringUtil.replaceAll(sub, "", str);
				start = 0;
				end = 0;
			}
		}
		return str;
	}
    public static String is2String(InputStream is){
        try {
            InputStreamReader reader = new InputStreamReader(is,"UTF-8");
            StringBuffer buffer = new StringBuffer();
            int b;
            while ((b=reader.read() )!= -1) {
                buffer.append((char)b);
            }
            return buffer.toString();
        } catch (IOException e) {
            e.printStackTrace();
            RaspberryLogger.getInstance().info(e.getMessage());
            return null;
        }
    }
    public static String date2String(Date date){
        String str = "";
        TimeZone t = TimeZone.getDefault();
        Calendar calendar = Calendar.getInstance(t);
        calendar.setTime(date);
        str = String.valueOf(calendar.get(Calendar.YEAR)) + "年" +
            String.valueOf(calendar.get(Calendar.MONTH)) + "月" +
            String.valueOf(calendar.get(Calendar.DATE)) + "日" +
            String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)) + "时" +
            String.valueOf(calendar.get(Calendar.MINUTE)) + "分" +
            String.valueOf(calendar.get(Calendar.SECOND)) + "秒" ;
        return str;
    }
    public static Date string2Date(String str){
    	return new Date();
    }
    public static String caculateDayOfWeek(Date date){
    	return null;
    }


}
